public class J101101 {
    // 2、无头双向链表实现
    public static class MyLinkedList {
        private static class Node{
            int val;
            Node next;
            Node pre;
            public Node(){
            }
            public Node(int val){
                this.val = val;
                pre = null;
                next = null;
            }
        }

        Node head = new Node();

        private MyLinkedList(){
            head = null;
        }

        //头插法
        public void addFirst(int data){
            Node newNode = new Node(data);
            if(head == null){
                head = newNode;
                return;
            }
            newNode.next = head;
            head.pre = newNode;
            head = newNode;
        }

        //尾插法
        public void addLast(int data){
            Node newNode = new Node(data);
            if(head == null){
                head = newNode;
                return;
            }
            Node cur = head;
            while(cur.next != null){
                cur = cur.next;
            }
            cur.next = newNode;
            newNode.pre = cur;
        }

        //任意位置插入,第一个数据节点为0号下标
        public boolean addIndex(int index,int data){
            if(index < 0 || index > size()){
                return false;
            }
            if(index == size()){
                addLast(data);
                return true;
            }
            if(index == 0){
                addFirst(data);
                return true;
            }
            Node newNode = new Node(data);
            Node cur = head;
            while(index > 0){
                cur = cur.next;
                index--;
            }
            cur.pre.next = newNode;
            newNode.pre = cur.pre;
            newNode.next = cur;
            cur.pre = newNode;
            return true;
        }

        //查找是否包含关键字key是否在单链表当中
        public boolean contains(int key){
            Node cur = head;
            while(cur != null){
                if(cur.val == key){
                    return true;
                }
                cur = cur.next;
            }
            return false;
        }

        //删除第一次出现关键字为key的节点
        public void remove(int key){
            Node cur = head;
            while(cur != null){
                if(cur.val == key){
                    if(cur.pre == null){
                        head = cur.next;
                        head.pre = null;
                    }else if(cur.next == null){
                        cur.pre.next = null;
                    }else {
                        cur.next.pre = cur.pre;
                        cur.pre.next = cur.next;
                    }
                    return;
                }
                cur = cur.next;
            }
        }

        //删除所有值为key的节点
        public void removeAllKey(int key){
            Node cur = head;
            while(cur != null){
                if(cur.val == key){
                    if(cur.pre == null){
                        head = cur.next;
                        head.pre = null;
                    }else if(cur.next == null){
                        cur.pre.next = null;
                    }else {
                        cur.next.pre = cur.pre;
                        cur.pre.next = cur.next;
                    }
                }
                cur = cur.next;
            }
        }

        //得到单链表的长度
        public int size(){
            int count = 0;
            Node cur = head;
            while(cur != null){
                count++;
                cur = cur.next;
            }
            return count;
        }

        public void display(){
            Node cur = head;
            System.out.print("[");
            while(cur != null){
                System.out.print(cur.val);
                if(cur.next != null){
                    System.out.print(",");
                }
                cur = cur.next;
            }
            System.out.println("]");
        }

        public void clear(){
            head = null;
        }
    }

    public static void main(String[] args) {
        MyLinkedList list = new MyLinkedList();
        list.addFirst(1);
        list.addLast(2);
        list.addIndex(0,0);
        list.addIndex(3,3);
        list.addFirst(3);
        list.addFirst(3);
        list.display();
        System.out.println(list.size());
        list.remove(3);
        list.display();
        System.out.println(list.size());
        list.removeAllKey(3);
        list.display();
        System.out.println(list.size());
        list.clear();
        list.display();
        System.out.println(list.size());
    }
}
